import {updatePageViews,updatePageViewsLive,updateEachPeriod,insertIntegral,insertWatchRecord} from '@/request/api'
export function formatDate (value) {
  const date = new Date(value)
  const y = date.getFullYear()
  let MM = date.getMonth() + 1
  MM = MM < 10 ? ('0' + MM) : MM
  let d = date.getDate()
  d = d < 10 ? ('0' + d) : d
  let h = date.getHours()
  h = h < 10 ? ('0' + h) : h
  let m = date.getMinutes()
  m = m < 10 ? ('0' + m) : m
  let s = date.getSeconds()
  s = s < 10 ? ('0' + s) : s
  return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
}
// 第一次查看，插入记录
export function oneWatchRecord (info,type) {
  // 课程
  const params = {
    "companyUserId": JSON.parse(localStorage.getItem('userInfo')).id,
    "id": '',
    "time": formatDate(new Date()),
    "type": type,
    "watchId": info.id
  }
  insertWatchRecord(params).then(res=> {
    if(res.code == 200) {

    }
  })
}
// 浏览量+1
export function getPageViews (info,type) {
  if(type==0) {
    // 课程
    const params = {
      "compleRate": info.compleRate,
      "createTime": formatDate(new Date()),
      "id": '',
      "pageViews": info.pageViews+1 || 1,
      "pid": info.id,
      "type": 0
    }
    updatePageViews(params).then(res=> {

    })
  }else if(type==1){
    // 直播
    const params = {
      // "createTime": '',
      "id": info.liveInfoId,
      "pageViews": info.pageViews+1 || 1,
      "pid": info.id,
      "type": info.type
    }
    updatePageViewsLive(params).then(res=> {

    })
  }else {
    // 资料
    // const params = {
    //   // "createTime": '',
    //   "id": info.infoId,
    //   "pageViews": info.pageViews+1 || 1,
    //   "pid": info.id,
    //   "type": info.type
    // }
    // updatePageViewsLive(params).then(res=> {
    //
    // })
  }
}
// 更新学时
export function updateEachPeriods (value) {
  const userInfo = JSON.parse(localStorage.getItem('userInfo'))
  const params = {
    "createTime": formatDate(new Date()),
    "del": userInfo.del,
    "email": userInfo.email,
    "id": userInfo.id,
    "idCard": userInfo.idCard,
    "integral": userInfo.integral,
    "job":userInfo.job,
    "nickname": userInfo.nickname,
    "period": value,
    "schoolName": userInfo.schoolName,
    "tel": userInfo.tel,
    "userName": userInfo.userName,
    "userStatus": userInfo.userStatus,
    "userType": userInfo.userType,
    "wechat": userInfo.wechat,
  }
  updateEachPeriod(params).then(res=> {
    userInfo.period = value
    localStorage.setItem('userInfo',JSON.stringify(userInfo))
  })
}
// 更新积分
export function insertIntegrals (value,id,name,type) {
  const userInfo = JSON.parse(localStorage.getItem('userInfo'))
  const params = {
    "companyUserId": JSON.parse(localStorage.getItem('userInfo')).id,
    "id": userInfo.id,
    "integral": value,
    "name": name,
    "pid": id,
    "time": formatDate(new Date()),
    "type": type //0:课程学习;1:直播学习;2:学习资料
  }
  insertIntegral(params).then(res=> {
    userInfo.integral = value
    localStorage.setItem('userInfo',JSON.stringify(userInfo))
  })
}

export default {
  formatDate,
  getPageViews,
  updateEachPeriods,
  insertIntegrals,
  oneWatchRecord
}
